package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.vo.AddReservationVo;
import com.zzyl.nursing.vo.CountByTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static com.zzyl.common.utils.UserThreadLocal.getUserId;

/**
 * 预约信息Service业务层处理
 * 
 * @author alexis
 * @date 2025-06-06
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param status 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> page(Integer status)
    {
        // 从ThreadLocal中获取当前登录用户
        Long createBy = UserThreadLocal.getUserId();
        return reservationMapper.page(status,  createBy);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }

    /**
     * 查询预约信息数量
     */
    @Override
    public void cancelledCpunt() {
        // 获取当天时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        // 从ThreadLocal中获取当前登录用户id
        Long userId = SecurityUtils.getUserId();

        // 查询当天取消预约的数量
        Long count = lambdaQuery()
                    .eq(Reservation::getStatus, 2)
                    .eq(Reservation::getCreateBy, userId)
                    .between(Reservation::getUpdateTime, startTime, endTime)
                    .count();

    }

    /**
     * 查询预约信息数量
     * @param time
     * @return
     */
    @Override
    public List<CountByTimeVo> countByTime(Long time) {
        //1. 获取查询的时间
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //2. 查询可预约的数量
        List<CountByTimeVo> list = reservationMapper.countByTime(startTime, endTime);
        return list;
    }

    /**
     * 添加预约信息
     * @param reservationVo
     */

    @Override
    public void addReservation(AddReservationVo reservationVo) {
        // 拷贝属性至数据库对象
        Reservation reservation = BeanUtil.toBean(reservationVo, Reservation.class);
        // 设置预约状态为待报道
        reservation.setStatus(0);
        // 设置创建时间
        reservation.setCreateTime(DateUtils.getNowDate());
        // 从ThreadLocal中获取当前用户id
        reservation.setCreateBy(getUserId().toString());
        // 保存预约状态信息
        reservationMapper.insertReservation(reservation);

    }

    /**
     * 取消预约信息
     * @param id
     */
    @Override
    public void cancle(Long id) {
        // 根据id查询预约信息
        Reservation reservation = reservationMapper.selectReservationById(id);
        // 将预约状态修改为2
        reservation.setStatus(2);
        // 设置修改时间
        reservation.setUpdateTime(DateUtils.getNowDate());
        // 设置更新人id
        reservation.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        // 根据id修改预约信息
        reservationMapper.updateReservation(reservation);
    }
}
